package com.example.backend.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class FileUploadController {
    @PostMapping("/show")
    public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {

        System.out.println(111);
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("文件为空");
        }

        String fileName = file.getOriginalFilename();
        String filePath = "C:/upload/" + fileName;  // 根据配置文件读取路径更佳

        try {
            file.transferTo(new File(filePath));
            return ResponseEntity.ok("/show/" + fileName);  // 返回访问路径
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("上传失败");
        }
    }
}